function calc(s : string) : int64;

var
	i, ls : longint;
	cur : string;
	st : vi; // vector<int>
	op : vc; // vector<char>
	ch : char;

	function isDelim(ch : char) : boolean;
	begin
		result := ch = ' ';
	end;

	procedure get(ch : char);

	var
		l, r : int64;

	begin
		r := st.back();
		st.pop();
		l := st.back();
		st.pop();
		if ch = '+' then st.push(l + r);
		if ch = '-' then st.push(l - r);
		if ch = '*' then st.push(l * r);
		if ch = '^' then st.push(pow(l, r));
	end;  

	function isOp(ch : char) : boolean;
	begin
		result := (ch = '+') or (ch = '-') or (ch = '*') or (ch = '^');
	end;

	function prior(ch : char) : longint;
	begin
		result := -1;
		if (ch = '+') or (ch = '-') then result := 1 else
		if ch = '*' then result := 2 else 
		if ch = '^' then result := 3;
	end;

begin 
	st.init();
	op.init();	
	ls := length(s);
	s := s + '$';
	i := 0;
	while i < ls do begin
		inc(i);
		ch := s[i];
		if not isDelim(ch) then begin
			if ch = 'x' then st.push(x) else              // identifier
			if ch = '(' then op.push('(') else
			if ch = ')' then begin
				while op.back() <> '(' do begin
					get(op.back());	
					op.pop();
				end;
				op.pop();
			end else
			if isOp(ch) then begin
				while (not op.empty()) and (prior(op.back()) >= prior(ch)) do begin
					get(op.back());
					op.pop();
				end;
				op.push(ch);
			end else begin
				cur := '';
				while s[i] in ['0'..'9'] do begin
					cur := cur + s[i];
					inc(i);
					if i > ls then break;
				end;
				dec(i);
				st.push(strtoint(cur));
			end;
		end;
	end;
	while not op.empty() do begin
		get(op.back());
		op.pop();
	end;
	result := st.back();
end;